/**
 * 
 */
package info.niwota.start;

import info.niwota.start.contact.Contact;

import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public class CallLogs {

	private CallLogs() {
	}

	public static void load(Context ctx, List<Contact> list, int max) {
		final ContentResolver cr = ctx.getContentResolver();
		final String by = android.provider.CallLog.Calls.DATE + " DESC";

		final Cursor c = cr.query(android.provider.CallLog.Calls.CONTENT_URI, null,
				null, null, by);
		// int idCol = c.getColumnIndex(android.provider.CallLog.Calls._ID);
		// int dateCol = c.getColumnIndex(android.provider.CallLog.Calls.DATE);
		// int durCol =
		// c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
		// int typeCol = c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
		int nameCol = c
				.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
		int numCol = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
		int numLabelCol = c
				.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_LABEL);
		try {
			if (!c.moveToFirst()) {
				return;
			}
			int count = 0;
			do {
				// String id = c.getString(idCol);
				// long date = c.getLong(dateCol);
				// long dur = c.getLong(durCol);
				String name = c.getString(nameCol);
				String num = c.getString(numCol);
				String numLabel = c.getString(numLabelCol);
				// int type = c.getInt(typeCol);
				// Drawable icon = null;
				// switch (type) {
				// case android.provider.CallLog.Calls.INCOMING_TYPE:
				// icon = res
				// .getDrawable(android.R.drawable.sym_call_incoming);
				// break;
				// case android.provider.CallLog.Calls.MISSED_TYPE:
				// icon = res.getDrawable(android.R.drawable.sym_call_missed);
				// break;
				// case android.provider.CallLog.Calls.OUTGOING_TYPE:
				// icon = res
				// .getDrawable(android.R.drawable.sym_call_outgoing);
				// break;
				// }
				
				if (num == null || exists(list, num)) {
					continue;
				}
				
				Contact ci = new Contact();
				ci.name = formatTitle(name, numLabel);
				ci.phone = num;
				ci.note = null;
				//
				list.add(ci);
				count++;
				if (count >= max) {
					break;
				}
			} while (c.moveToNext());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			c.close();
		}
	}
	
	private static boolean exists(List<Contact> list, String num) {
		for (Contact c: list) {
			if (num.equals(c.phone)) {
				return true;
			}
		}
		return false;
	}

	private static String formatTitle(String name, String numLabel) {
		final String SP = " ";
		StringBuilder sb = new StringBuilder();
		if (!TextUtils.isEmpty(name)) {
			sb.append(name);
			sb.append(SP);
		}
		if (!TextUtils.isEmpty(numLabel)) {
			sb.append(numLabel);
			sb.append(SP);
		}
		return sb.toString();
	}

}
